#使用paddle2.5.2基础镜像
FROM registry.baidubce.com/paddlepaddle/paddle:2.5.2
#由于typing-extensions版本过低，安装paddlehub要求版本要大于4.6.1，所以先卸载老版本在安装
RUN pip3 uninstall typing-extensions -y
RUN pip3 install typing-extensions==4.6.1
#更新pip并安装paddlehub
RUN pip install --upgrade pip
RUN pip3.7 install paddlehub==2.1.0 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
# 下载2.6版本的PaddleOCR代码
RUN git clone -b release/2.6 https://gitee.com/PaddlePaddle/PaddleOCR.git /PaddleOCR
 
WORKDIR /PaddleOCR
#安装依赖包
RUN pip3 install -r requirements.txt -i  https://mirrors.aliyun.com/pypi/simple/
#创建模型目录并下载解压模型
RUN mkdir -p /PaddleOCR/inference/
# OCR 文本检测配置
ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar /PaddleOCR/inference/
# OCR 文本方向分类模型
ADD https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar /PaddleOCR/inference/
# OCR 文本识别模型
ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar /PaddleOCR/inference/
 
RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_det_infer.tar -C /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_ppocr_mobile_v2.0_cls_infer.tar -C /PaddleOCR/inference/
RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_rec_infer.tar -C /PaddleOCR/inference/
# 由于安装时提示protobuf版本过高，卸载高版本安装对应版本
RUN pip uninstall protobuf -y
RUN pip install protobuf==3.20.2 -i  https://mirrors.aliyun.com/pypi/simple/
# 运行时发现缺少bidi模块
RUN apt-get update
#RUN chmod 777 ./rustup-init.sh
RUN apt-get install -y cargo
RUN pip install python-bidi -i  https://mirrors.aliyun.com/pypi/simple/
# 由于paddlepaddle2.5.0后没有fluid了，所以需要调整下面文件的内容
RUN sed -i 's/from paddle.fluid.layers import RNNCell, LSTMCell, rnn, BeamSearchDecoder, dynamic_decode/from paddle.nn import RNNCellBase, LSTMCell, RNN, BeamSearchDecoder, dynamic_decode /g' /usr/local/lib/python3.7/dist-packages/paddlehub/compat/task/text_generation_task.py
 
RUN sed -i 's/(RNNCell)/(RNNCellBase)/g' /usr/local/lib/python3.7/dist-packages/paddlehub/compat/task/text_generation_task.py
 
EXPOSE 8866
# 安装ocr_system服务并启动
CMD ["/bin/bash","-c","hub install deploy/hubserving/ocr_system/ && hub serving start -m ocr_system"]